package ruixinyi.work5;

import com.rabbitmq.client.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.concurrent.TimeoutException;
import com.rabbitmq.client.Channel;
public class Consumer {
    private static final String DB_URL = "jdbc:mysql://localhost:3306/log";  // 修改为实际数据库名
    private static final String DB_USER = "root";  // 数据库用户名
    private static final String DB_PASSWORD = "root";  // 数据库密码

    public static void main(String[] args) throws IOException, TimeoutException, SQLException {
        ConnectionFactory rabbitMQFactory = new ConnectionFactory();
        rabbitMQFactory.setHost("localhost");

            // 获取RabbitMQ的连接
        com.rabbitmq.client.Connection rabbitMQConnection = rabbitMQFactory.newConnection();
            // 通过RabbitMQ连接创建通道
        Channel channel = rabbitMQConnection.createChannel();

        String queue1 = "log_queue_1";
        String queue2 = "log_queue_2";

        channel.queueDeclare(queue1, false, false, false, null);
        channel.queueDeclare(queue2, false, false, false, null);

        // 这里可以添加更多队列声明，如果有更多生产者对应的队列

        // 创建数据库连接
        Connection dbConnection = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);

        // 消费消息逻辑，按顺序处理不同队列消息
        DeliverCallback deliverCallback1 = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            try {
                // 插入数据库操作，这里简单示例插入到名为logs的表中，表结构可自行设计，至少包含message字段
                String sql = "INSERT INTO logs (message) VALUES (?)";
                PreparedStatement statement = dbConnection.prepareStatement(sql);
                statement.setString(1, message);
                statement.executeUpdate();
                System.out.println("Stored log message from queue 1: " + message);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        };

        DeliverCallback deliverCallback2 = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody(), "UTF-8");
            try {
                String sql = "INSERT INTO logs (message) VALUES (?)";
                PreparedStatement statement = dbConnection.prepareStatement(sql);
                statement.setString(1, message);
                statement.executeUpdate();
                System.out.println("Stored log message from queue 2: " + message);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        };

        channel.basicConsume(queue1, true, deliverCallback1, consumerTag -> { });
        channel.basicConsume(queue2, true, deliverCallback2, consumerTag -> { });

        // 保持程序运行，持续监听消息
        while (true) {
            // 可以添加一些逻辑来合理控制循环和资源等，这里简单循环等待消息
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}